package com.baidu.infinity.ui.fragment.home.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.SweepGradient
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.annotation.RequiresApi
import com.baidu.infinity.ui.fragment.home.shape.LayerManager
import com.baidu.infinity.ui.util.dp2px
import com.baidu.infinity.ui.util.dp2pxF
import com.baidu.infinity.ui.util.toast

class ColorPickerView(
    context: Context,
    attrs: AttributeSet?
): View(context,attrs) {
    val defaultWidth = context.dp2px(200)
    val defaultHeight = context.dp2px(200)
    private var centerX: Float = 0f
    private var centerY: Float = 0f
    private var mTouchX: Float = 0f
    private var mTouchY: Float = 0f
    val mPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
        color = LayerManager.instance.mColor
    }
    val mSelectPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.FILL
        color = Color.WHITE
        setShadowLayer(20f,0f,0f,Color.BLACK)

    }
    var colorChangeListener: (color: Int)->Unit = {}
    //色相值
    private var mHue = 0f
    //饱和度
    private var mSturation = 1f
    //明度
    private var mTightness = 1f
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        var desireWidth = MeasureSpec.getSize(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var desireHeight = MeasureSpec.getSize(heightMeasureSpec)
        if(widthMode != MeasureSpec.EXACTLY){
            desireWidth = defaultWidth
        }
        if(heightMode != MeasureSpec.EXACTLY){
            desireHeight = defaultHeight
        }
        centerX = (desireWidth / 2).toFloat()
        centerY = (desireHeight / 2).toFloat()
        mTouchX = centerX
        mTouchY = centerY
        setMeasuredDimension(desireWidth,desireHeight)
    }

    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mPaint.shader = SweepGradient(centerX,centerY,
            intArrayOf(
                0xFFFF0000.toInt(),
                0xFFFFFF00.toInt(),
                0xFF00FF00.toInt(),
                0xFF00FFFF.toInt(),
                0xFF0000FF.toInt(),
                0xFFFF00FF.toInt(),
                0xFFFF0000.toInt()
            ),
            floatArrayOf(
                1f / 6 * 0,
                1f / 6 * 1,
                1f / 6 * 2,
                1f / 6 * 3,
                1f / 6 * 4,
                1f / 6 * 5,
                1f
            )
        )
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawColorCircle(canvas)
        drawSelectCircle(canvas)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN,MotionEvent.ACTION_MOVE -> {
                if(inCircle(event.x - centerX,event.y - centerY,width/2)){
                    mTouchX =event.x
                    mTouchY = event.y
                    val radians = Math.atan2((mTouchY - centerY).toDouble(),(mTouchX - centerX).toDouble())
                    var degree = Math.toDegrees(radians)
                    if(degree < 0){
                        degree = 360 - Math.abs(degree)
                    }
                    mHue = degree.toFloat()
                    invalidate()
                }
                return true
            }


        }
        return super.onTouchEvent(event)
    }
    fun addColorChangeListener(listener: (color:Int)->Unit){
        colorChangeListener = listener
    }
    fun drawColorCircle(canvas: Canvas){
        canvas.drawCircle(centerX,centerY,centerX,mPaint)
    }
    fun drawSelectCircle(canvas: Canvas){
        val color = Color.HSVToColor(floatArrayOf(
            mHue,
            mSturation,
            mTightness
        ))
        mSelectPaint.color = color
        colorChangeListener(color)
        canvas.drawCircle(mTouchX,mTouchY,dp2pxF(30),mSelectPaint)
    }
    fun changeSturation(progress: Int){
        mSturation = progress / 100f
        val color = Color.HSVToColor(floatArrayOf(
            mHue,
            mSturation,
            mTightness
        ))
        mSelectPaint.color = color
        colorChangeListener(color)
        invalidate()
    }
    fun changeTightness(progress: Int){
        mTightness = progress / 100f
        val color = Color.HSVToColor(floatArrayOf(
            mHue,
            mSturation,
            mTightness
        ))
        mSelectPaint.color = color
        colorChangeListener(color)
        invalidate()
    }
    private fun inCircle(x: Float,y: Float,r: Int): Boolean{
        return (x*x + y*y) <= r*r
    }
}